<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\SysUserFeedback;

/**
 * SysUserFeedbackSearch represents the model behind the search form about `backend\models\SysUserFeedback`.
 */
class SysUserFeedbackSearch extends SysUserFeedback
{
    /**
     * @inheritdoc
     */
	public $username;
	public $feedback_time;
    public function rules()
    {
        return [
            [['id', 'uid', 'time'], 'integer'],
            [['title', 'content'], 'safe'],
            [['username','feedback_time'], 'string', 'max' => 128],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = SysUserFeedback::find()->joinWith(['usernameByRead']);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $dataProvider->setSort([
        		'attributes' => [
        		'id',
        		'uid',
        		'time',
        		'title',
        		'content',
       
        		'username'=>[
        		'asc' => ['user_info.username' =>SORT_ASC],
        		'desc' => ['user_info.username' =>SORT_DESC],
        		'label'=> Yii::t('app','Username')
        				],
        		'feedback_time'=>[
        		'asc' => ['time' =>SORT_ASC],
        		'desc' => ['time' =>SORT_DESC],
        				'label'=> Yii::t('app','Feedback Time')
        			],
        				 
        			],
        		]);
        
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'sys_user_feedback.id' => $this->id,
            'uid' => $this->uid,
            'time' => $this->time,
        ]);

        $query->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'content', $this->content])
        	->andFilterWhere(['like','user_info.username',$this->username])
        	->andFilterWhere(['like','FROM_UNIXTIME(time, "%Y-%m-%d %H:%i:%S")',$this->feedback_time])
        	;

        return $dataProvider;
    }
}
